package com.wva.request;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Enumeration;

/**
 * 演示Request对象获取   login.html
 *      获取请求行
 *      获取请求头
 *      获取请求消息体
 */
@WebServlet("/demo01request")
public class Demo01Request extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("doPost.....");
        //获取请求消息体--请求参数
        //1.获取字符流
        BufferedReader br = request.getReader();
        //2.读取数据
        String line = null;
        while((line = br.readLine()) != null){
            System.out.println(line);
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) {
        requestLine(request);  //获取请求行
        requestHeader(request);  //获取请求头
    }


    private void requestHeader(HttpServletRequest request) {
        /**
         *  演示获取请求头数据
         */
        //1.获取所有请求头名称
        Enumeration<String> headerNames = request.getHeaderNames();
        //2.遍历
        while(headerNames.hasMoreElements()){
            String name = headerNames.nextElement();
            //根据名称获取请求头的值
            String value = request.getHeader(name);
            System.out.println(name+"---"+value);
        }
    }

    private void requestLine(HttpServletRequest request) {
        /**
         * 请求行数据
         * String getMethod()      // 获取请求方式 ：GET
         * String getContextPath() // (*)获取虚拟目录：/day14
         * String getServletPath() // 获取Servlet路径: /demo1
         * String getQueryString() // (*)获取get方式请求参数：name=zhangsan
         * String getRequestURI()  // (*)获取请求URI：/day14/demo1
         * StringBuffer getRequestURL() // (*)获取全路径: http://localhost/day14/demo1
         * String getProtocol()    //获取协议及版本：HTTP/1.1
         * String getRemoteAddr()  //获取客户机的IP地址：
         */
        //1. 获取请求方式 ：GET
        String method = request.getMethod();
        System.out.println(method);  // GET
        //2.(*)获取虚拟目录：/day14
        String contextPath = request.getContextPath();
        System.out.println(contextPath); //19_tomcat
        //3. 获取Servlet路径: /demo1
        String servletPath = request.getServletPath();
        System.out.println(servletPath); ///demo01request
        //4. 获取get方式请求参数：name=zhangsan
        String queryString = request.getQueryString();
        System.out.println(queryString); //null
        //5.(*)获取请求URI：/day14/demo1
        String requestURI = request.getRequestURI();
        StringBuffer requestURL = request.getRequestURL();
        System.out.println(requestURI); // 19_tomcat/demo01request
        System.out.println(requestURL); // http://localhost:8080/19_tomcat/demo01request
        //6. 获取协议及版本：HTTP/1.1
        String protocol = request.getProtocol();
        System.out.println(protocol);  //HTTP/1.1
        //7. 获取客户机的IP地址：
        String remoteAddr = request.getRemoteAddr();
        System.out.println(remoteAddr);  // 0:0:0:0:0:0:0:1
    }
}
